package com.lorenzogao.latte_core.net


import android.content.Context
import com.lorenzogao.latte_core.net.callback.IError
import com.lorenzogao.latte_core.net.callback.IFailure
import com.lorenzogao.latte_core.net.callback.IRequest
import com.lorenzogao.latte_core.net.callback.ISuccess
import com.lorenzogao.latte_core.ui.LoaderStyle
import okhttp3.MediaType
import okhttp3.RequestBody
import java.io.File
import java.lang.IllegalArgumentException

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2020-01-08 13:23
 *  description:
 */

class RestClientBuilder {

    private var mParams = RestCreator.PARAMS

    private var mRequest: IRequest? = null

    private var mSuccess: ISuccess? = null

    private var mFailure: IFailure? = null

    private var mError: IError? = null

    private var mBody: RequestBody? = null

    private var mUrl: String = ""

    private var mContext: Context? = null

    private var mLoaderStyle: LoaderStyle? = null

    private var mFile: File? = null

    private var mDir: String? = null

    private var mExtension: String? = null

    private var mName: String? = null


    fun with(context: Context): RestClientBuilder {
        this.mContext = context
        return this
    }


    fun loaderStyle(loaderStyle: LoaderStyle): RestClientBuilder {
        this.mLoaderStyle = loaderStyle
        return this
    }


    fun loader(context: Context, style: LoaderStyle): RestClientBuilder {
        this.mContext = context
        this.mLoaderStyle = style
        return this
    }

    fun loader(context: Context): RestClientBuilder {
        loader(context, LoaderStyle.BallClipRotatePulseIndicator)
        return this
    }


    fun url(url: String): RestClientBuilder {
        this.mUrl = url
        return this
    }


    fun params(params: MutableMap<String, Any>): RestClientBuilder {
        this.mParams.putAll(params)
        return this
    }


    fun params(key: String, value: Any): RestClientBuilder {
        this.mParams[key] = value
        return this
    }

    fun file(file: File): RestClientBuilder {
        this.mFile = file
        return this
    }

    fun file(file: String): RestClientBuilder {
        this.mFile = File(file)
        return this
    }


    fun raw(raw: String): RestClientBuilder {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw)
        return this
    }

    fun dir(dir: String): RestClientBuilder {
        this.mDir = dir
        return this
    }

    fun extension(extension: String): RestClientBuilder {
        this.mExtension = extension
        return this
    }

    fun name(name: String): RestClientBuilder {
        this.mName = name
        return this
    }


    fun success(success: ISuccess): RestClientBuilder {
        this.mSuccess = success
        return this
    }

    fun error(error: IError): RestClientBuilder {
        this.mError = error
        return this
    }

    fun failure(failure: IFailure): RestClientBuilder {
        this.mFailure = failure
        return this
    }

    fun request(request: IRequest): RestClientBuilder {
        this.mRequest = request
        return this
    }


    fun builder(): RestClient {
        require(!mUrl.isNullOrEmpty()) { "url is not be null" }
        return RestClient(
            mParams,
            mRequest,
            mSuccess,
            mFailure,
            mError,
            mBody,
            mUrl,
            mLoaderStyle,
            mContext,
            mFile,
            mDir,
            mExtension,
            mName
        )
    }

}